package com.flag.liutao.modules.student.controller;


import com.flag.liutao.common.domain.IdParam;
import com.flag.liutao.common.domain.IdsParam;
import com.flag.liutao.common.domain.ResponseResult;
import com.flag.liutao.modules.student.param.ClassParam;
import com.flag.liutao.modules.student.param.ClassesQueryParam;
import com.flag.liutao.modules.student.service.IClassesService;
import com.flag.liutao.modules.student.service.IDepartmentService;
import com.flag.liutao.modules.student.vo.DepartmentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author godM
 * @since 2022-12-06
 */
@RestController
@RequestMapping("/admin/classes")
@RequiredArgsConstructor
@Api(tags = "班级管理")
public class ClassesController {
    private final IClassesService classesService;
    private final IDepartmentService departmentService;

    @GetMapping("page")
    @ApiOperation(value="分页获取班级信息列表",notes = "班级管理：分页获取班级信息列表")
    @PreAuthorize("hasAuthority('sys_power:class_R')")
    public ResponseResult<Map<String,Object>> page(ClassesQueryParam param) {
        Map<String,Object> map = classesService.getPage(param.getPage(),param.getLimit(),param.getKeyword(),param.getMajorId(),param.getGradeId());
        return ResponseResult.ok(map);
    }
    @GetMapping("list")
    @ApiOperation(value="获取全部班级信息列表",notes = "班级管理：获取全部班级信息列表")
    @PreAuthorize("hasAuthority('sys_power:class_R')")
    public ResponseResult<List<DepartmentVo>> getClassList(){
        return ResponseResult.ok(departmentService.getDeptList(2));
    }
    @PostMapping("add")
    @ApiOperation(value="添加班级信息",notes = "班级管理：添加班级信息")
    @PreAuthorize("hasAuthority('sys_power:class_C')")
    public ResponseResult<Boolean> addClass(@Validated @RequestBody ClassParam param){
        classesService.addClass(param);
        return ResponseResult.ok();
    }
    @PostMapping("edit")
    @ApiOperation(value="修改班级信息",notes = "班级管理：修改班级信息")
    @PreAuthorize("hasAuthority('sys_power:class_U')")
    public ResponseResult<Boolean> editClass(@Validated @RequestBody ClassParam param){
        classesService.editClass(param);
        return ResponseResult.ok();
    }

    @PostMapping("del")
    @ApiOperation(value="删除班级信息",notes = "班级管理：删除班级信息")
    @PreAuthorize("hasAuthority('sys_power:class_D')")
    public ResponseResult<Boolean> delClass(@Validated @RequestBody IdParam param){
        classesService.delClass(param.getId());
        return ResponseResult.ok();
    }

    @PostMapping("dels")
    @ApiOperation(value="批量删除班级信息",notes = "班级管理：批量删除班级信息")
    @PreAuthorize("hasAuthority('sys_power:class_D')")
    public ResponseResult<Boolean> delsClass(@Validated @RequestBody IdsParam param){
        for (Long id : param.getIds()) {
            classesService.delClass(id);
        }
        return ResponseResult.ok();
    }

}
